/* GET home page. */

var crypto = require('crypto');
var User = require('../models/user.js');
var Post = require('../models/post.js');

exports.index = function(req, res){
    Post.get(null,function(err,posts){
        if(err){
            posts = [];
        }
        res.render('index',{
            title:'首页',
            posts:posts
        })
    });
  //res.render('index', { title: 'Express' });
};

exports.regst = function(req, res){
    res.render('reg', { title: '用户注册' });
};


exports.regster = function(req, res){
    if(req.body['password-repeat'] != req.body['password']){
        req.flash('error','密码不一致');
        return res.redirect('/reg');
    }

    var md5 = crypto.createHash('md5');
    var password = md5.update(req.body.password).digest('base64');
    //console.log(password);
    //console.log(req.body.username);
    var newUser = new User({
        name:req.body.username,
        password:password,
    });

    //检查用户名是否已存在
    User.get(newUser.name,function(err,user){
        //console.log(req.body.username);
        //console.log(newUser.name);
       if(user){
           err = '用户名已存在！';
       }
       if(err){
           req.flash('error',err);
           return res.redirect('/reg');
       }
       //用户不存在，新增用户
        newUser.save(function(err){

           //console.log(err);
           //console.log(newUser);

          if(err){
              console.log(err);
               req.flash('error',err);
               return res.redirect('/reg');
           }

           req.session.user = newUser;
           console.log(err);
           //req.session.message('注册成功');
           req.flash('success','注册成功');
           // console.log('注册成功');
           res.redirect('/');
       });
    });
};

exports.login = function(req, res){
    //console.log('yyy');
    res.render('login', { title: '用户登录' });
}

exports.doLogin = function(req,res){

    var md5 = crypto.createHash('md5');
    var password = md5.update(req.body.password).digest('base64');

    User.get(req.body.username,function(err,user){
        if(!user){
            console.log(error);
            req.flash('error','用户名不存在');
            return res.redirect('/login');
        }
        if(user.password != password){
            console.log('密码错误');
            req.flash('error','密码错误');
            return res.redirect('/login');
        }
        req.session.user = user;
        console.log('登陆成功');
        req.flash('success','登陆成功');
        res.redirect('/');
    });

    /*if(req.body.username === userMsg.username && req.body.password === userMsg.password){

        req.session.user = userMsg;
        //console.log(req.session.user);
        //req.session.user = userMsg;
        return res.redirect('/home');
    }else{
        req.session.error = '用户名或密码不正确';
        return res.redirect('/login');
    }*/
};

exports.logout = function(req,res){
    req.session.user = null;
    console.log('退出成功');
    res.redirect('/');
}

exports.homepage = function(req,res){
    console.log(req.session.user.name);
    res.render('home', { title: 'Home',user:req.session.user.name});
    //res.render('home',{title:'主页',user:userMsg.username});

}


exports.addblog = function(req,res){
    res.render('addblog', { title: '发表微博',user:req.session.user.name});
}

exports.doAddblog = function(req,res){
    var currentUser = req.session.user;
    var post = new Post(currentUser.name,req.body.post);
    post.save(function(err){
        if(err){
            req.flash('error',err);
            return res.redirect('/');
        }
        req.flash('success','发表成功');
        res.redirect('/u/' + currentUser.name);
    });
}

exports.publishBlog = function(req,res){
    User.get(req.params.user,function(err,user){
        if(!user){
            req.flash('error','用户不存在');
            return res.redirect('/');
        }
        Post.get(user.name,function(err,posts){
            if(!user) {
                req.flash('error', err);
                return res.redirect('/');
            }
            res.render('user',{
                title:user.name,
                posts:posts
            });
        });
    });
}


















